﻿
var gPluginPath=external.m2_plugin_folder(SECURITY_ID,PLUGIN_NAME);

var gLastClickSignID=1;
$(function(){

//    CreateSignList();
	var html=[];
	for(var i=1;i<=2*pharaoh_Config.Y;i++)
	{
		var type="empty";
		if(ReadSign(i))
		{
			type="";
		}
		html.push("<li class='"+type+"' onclick='GroupClick(this);' id='sign_"+i+"'>Sign"+i+"</li>");
	}
	$("#groupList").html(html.join(""));
    $('#sign_1').addClass('current');
    LoadSign(1);
});

function GroupClick(o)
{
	var curSignID=o.id.split("_")[1];
    if(curSignID==gLastClickSignID) {return;}
    //alert("sign_"+gLastClickSignID);
	//document.getElementById("sign_"+gLastClickSignID).className="";
    $('#sign_'+gLastClickSignID).removeClass('current');
    gLastClickSignID=curSignID;
    //o.className='current';
    $('#sign_'+gLastClickSignID).addClass('current');
    LoadSign(gLastClickSignID);	
}

function groupDescriptionChanged()
{
	var txt=document.getElementById("groupDescription").value;
	WriteSign(gLastClickSignID,txt);
	if(txt)
	{
		$("#sign_"+gLastClickSignID).removeClass("empty");
	}
	else
	{
		$("#sign_"+gLastClickSignID).addClass("empty");
	}
}


function LoadSign(signID)
{
	var signText=ReadSign(signID);
	if(signText)
	{
		document.getElementById("groupDescription").value=signText;
	}
	else
	{
		document.getElementById("groupDescription").value="";	
	}
}

function ReadSign(signID)
{
	var signFn='userData\\Sign\\sign'+signID+".txt";
	var signText = external.readFile(SECURITY_ID,PLUGIN_NAME,signFn);
	if(signText)
	{
		return clear0(signText);
	}
	else
	{
		return "";
	}
}

	var fso=external.max_activex(SECURITY_ID,"Scripting.FileSystemObject");
function WriteSign(signID,txt)
{

	var signFn='userData\\Sign\\sign'+signID+".txt";
	external.writeFile(SECURITY_ID,PLUGIN_NAME,signFn,txt);
	if(!txt)
	{
		fso.DeleteFile(gPluginPath+signFn,true);
	}
}